package com.fml.leecode.eaysy;

import com.fml.dataStructure.sort.util.SortUtils;

/**
 * 编写一个函数，其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。
 * <p>
 * 不要给另外的数组分配额外的空间，你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：s = ["h","e","l","l","o"]
 * 输出：["o","l","l","e","h"]
 * 示例 2：
 * <p>
 * 输入：s = ["H","a","n","n","a","h"]
 * 输出：["h","a","n","n","a","H"]
 * <p>
 * <p>
 * 提示：
 * <p>
 * 1 <= s.length <= 105
 * s[i] 都是 ASCII 码表中的可打印字符
 */
public class Demo344 {


    public static void main(String[] args) {
        char[] s = {'h', 'e', 'l', 'l', 'o'};

        //reverse01(s);
       // reverse02(s);
        String a = "zxcdddqweqwewe123";

        String b  =  reverseStr(a);
        System.out.println(b);

    }


    public static String reverseStr(String s) {
        if (s.length() <= 1) {
            return s;
        }
        return reverseStr(s.substring(1)) + s.charAt(0);
    }

    /**
     * 循环解法
     * @param s
     */
    private static void reverse02(char[] s) {

        for (int i = 0; i < s.length; i++) {

            if (i >= s.length / 2) {
                break;
            }
            char temp = s[i];
            s[i] = s[s.length - 1 - i];
            s[s.length - 1 - i] = temp;
        }
    }

    /**
     * 解法1  递归
     * @param s
     */
    private static void reverse01(char[] s) {
        reverse(s);
        for (char c : s) {
            System.out.println(c);
        }
    }

    /**
     * 递归
     * @param s
     */
    public static void reverse(char[] s) {
        if (s == null || s.length <= 1) {
            return;
        }
        reverseHelp(s, 0, s.length - 1);
    }

    private static void reverseHelp(char[] s, int start, int end) {
        //基线条件
        if (start >= end) {
            return;
        }
        //首尾交换
        char temp = s[start];
        s[start] = s[end];
        s[end] = temp;

        //递归剩余部分处理
        reverseHelp(s, start + 1, end - 1);
    }


}
